<template>
  <div class="music-list">
    <!-- 左上角返回ICON图标 -->
    <div class="back" @click="$router.back">
      <i class="icon-back"></i>
    </div>
    <h1 class="title">{{ title }}</h1>
    <div ref="bgImgRef"
         :style="bgImageStyle"
         class="bg-image">
      <div :style="filterStyle"
           class="filter"></div>
    </div>

    <!-- 歌曲列表区域 -->
    <!-- 给Scroll设置了 top值, 这个div.list容器就固定, 就实现了滚动效果 -->
    <Scroll
      v-loading="loading"
      v-no-result:[noResultText]="noResult"
      :probe-type="3"
      :style="scrollStyle"
      class="list"
      @scroll="onScroll">
      <div class="song-list-wrapper">
        <songList
          :songs="songs">
        </songList>
      </div>
    </Scroll>
  </div>
</template>

<script>
import Scroll from '../base/scroll/Scroll'
import songList from '../base/song-list/songList'

const RESERVED_HEIGHT = 40
export default {
  name: 'musicList',
  components: {
    Scroll,
    songList
  },
  props: {
    songs: {
      type: Array,
      default: () => {
        return []
      }
    },
    pic: String,
    title: String,
    loading: {
      type: Boolean,
      default: false
    },
    noResultText: {
      type: String,
      default: '抱歉, 没有找到可播放的歌曲'
    }
  },
  data () {
    return {
      imageHeight: 0,
      scrollY: 0,
      maxTranslateY: 0    // 最大可用滚动的距离
    }
  },
  computed: {
    bgImageStyle () {
      const scrollY = this.scrollY
      let zIndex = 0
      let paddingTop = '70%'
      let height = 0
      // 解决移动端兼容性问题, IOS兼容性问题
      let translateZ = 0

      // 向上推, 歌曲列表覆盖图片
      if (scrollY > this.maxTranslateY) {
        zIndex = 10
        paddingTop = 0
        height = `${RESERVED_HEIGHT}px`
        translateZ = 1
      }

      // 向下拉缩放图片
      let scale = 1
      if (scrollY < 0) {
        scale = 1 + Math.abs(scrollY / this.imageHeight)
      }

      return {
        paddingTop,
        height,
        zIndex,
        backgroundImage: `url(${this.pic})`,
        transform: `scale(${scale}) translateZ(${translateZ}px)`
      }
    },
    scrollStyle () {
      return {
        top: `${this.imageHeight}px`
      }
    },
    filterStyle () {
      let blur = 0
      // todo  多次出现this.scrollY, 定义局部变量做缓存的意义?
      const scrollY = this.scrollY
      const imageHeight = this.maxTranslateY
      // 向上推, 歌曲列表覆盖图片
      if (scrollY >= 0) {
        blur = Math.min(this.maxTranslateY / imageHeight, scrollY / imageHeight) * 20
      }
      return {
        backdropFilter: `blur(${blur}px)`
      }
    },
    noResult () {
      return !this.loading && !this.songs.length
    }
  },
  mounted () {
    this.imageHeight = this.$refs.bgImgRef.clientHeight
    this.maxTranslateY = this.imageHeight - RESERVED_HEIGHT

  },
  methods: {
    onScroll (pos) {
      this.scrollY = -pos.y
    }
  }
}
</script>

<style lang="scss" scoped>

.music-list {
  .back {
    position: absolute;
    top: 0;
    left: 6px;
    z-index: 20;
    transform: translateZ(2px);
    height: 42px;
    width: 42px;

    padding: 10px;
    box-sizing: border-box;
    font-size: $font-size-large-x;
    color: $color-theme;
  }

  .title {
    position: absolute;
    top: 0;
    left: 10%;
    width: 80%;
    z-index: 20;
    transform: translateZ(2px);

    // 文本长度溢出显示省略号
    @include el();
    //text-overflow: ellipsis;
    //overflow: hidden;
    //white-space: nowrap;


    height: 40px;
    line-height: 40px;
    text-align: center;
    font-size: $font-size-large;
    color: $color-text;
  }

  .bg-image {
    position: relative;
    // transform-origin属性允许改变被旋转元素的位置
    // 注意: 该属性必须与 `transform` 属性一同使用
    transform-origin: top;
    background-size: cover;

    // 实现 图片宽和高比例是 10 : 7
    padding-top: 70%;
    height: 0;
    width: 100%;

    // 如何理解这个filter的作用
    .filter {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(7, 17, 27, 0.4);
    }
  }

  .list {
    // 与 v-loading 自定义指令组件组成子绝互相, 实现了v-loading在 div.list中间
    position: absolute;
    bottom: 0;
    width: 100%;
    z-index: 0;
    //overflow: hidden;

    .song-list-wrapper {
      background-color: $color-background;
      padding: 20px 30px;
    }
  }
}


</style>
